home *** CD-ROM | disk | FTP | other *** search
- /* Functions for opening and closing files. This follows the guidelines
- published in one of the TechNotes for avoiding the dangers of using
- the reference number of a closed file; when a file is closed, the
- file's reference number is set to the constant FILE_CLOSED.
-
- 93/03/12 AIH
- - The volume is flushed after a file is closed
-
- 91/11/14 AIH
- - Removed checks for system software version 7.0
-
- 91/05/08 AIH
- - HOpenDF is used if it's available
-
- 91/04/23 Ari Halberstadt (AIH)
- - Aliases are resolved before a file is opened. */
-
- #include "MacLib.h"
- #include "FileLib.h"
-
- /* open the data fork of the file using the specified permissions */
- void FileOpen(FileType *fp, FilePermType perm)
- {
- FileRefType ref = FILE_CLOSED;
- OSErr err = noErr;
-
- require(FileValid(fp));
- require(fp->ref == FILE_CLOSED);
-
- /* resolve aliases */
- FileResolve(fp);
-
- /* try to open the file with the specified permission */
- if (MacVersion() >= 0x0700)
- err = HOpenDF(fp->vol, fp->dir, fp->pnm, perm, &ref);
- else
- err = HOpen(fp->vol, fp->dir, fp->pnm, perm, &ref);
-
- /* if the file system doesn't recognize the permission then translate
- it into something the file system is more likely to understand and
- retry */
- if (err == paramErr) {
- if (MacVersion() >= 0x0700)
- err = HOpenDF(fp->vol, fp->dir, fp->pnm, fsCurPerm, &ref);
- else
- err = HOpen(fp->vol, fp->dir, fp->pnm, fsCurPerm, &ref);
- }
- FailOSErr(err);
- fp->ref = ref;
- ensure(fp->ref != FILE_CLOSED);
- }
-
- /* open the resource fork of the file using the specified permissions */
- void FileOpenRes(FileType *fp, FilePermType perm)
- {
- FileRefType ref = FILE_CLOSED;
- OSErr err = noErr;
-
- require(FileValid(fp));
- require(fp->ref == FILE_CLOSED);
- FileResolve(fp);
- err = HOpenRF(fp->vol, fp->dir, fp->pnm, perm, &ref);
- if (err == paramErr)
- err = HOpenRF(fp->vol, fp->dir, fp->pnm, fsCurPerm, &ref);
- FailOSErr(err);
- fp->ref = ref;
- ensure(fp->ref != FILE_CLOSED);
- }
-
- /* close the file */
- void FileClose(FileType *fp)
- {
- require(FileValid(fp));
- if (fp->ref != FILE_CLOSED) {
- FailOSErr(FSClose(fp->ref));
- fp->ref = FILE_CLOSED;
- FileFlush(fp);
- }
- ensure(fp->ref == FILE_CLOSED);
- }
-